import pickle

with open('goo.dat','wb') as f:
    pickle.dump([1,2,3],f)  # dump 向文件中写入对象
    pickle.dump('Hello', f)
    pickle.dump(3.141592, f)

# with open('goo.dat','rb') as file:
#     a = pickle.load(file)    # load 从文件中读取对象
#     b = pickle.load(file)
#     c = pickle.load(file)
#     print(type(a),a)
#     print(type(b),b)
#     print(type(c),c)


# 当你打开一个文件，并不知道这个文件中有多少个对象时，可以加载尽可能多的对象，直到发生 EOFError错误（文件已达到末尾）
loaded = []
with open('goo.dat','rb') as fs:
    while True:
        try:
            item = pickle.load(fs)
        except EOFError:
            print('Loaded',len(loaded),'items.')
            break
        print(type(item),item)
        loaded.append(item)



